package com.slz3.business.module.configManagement.conShareManage.sync;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.slz3.business.frame.module.DomainEntity;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.module.configManagement.conShareManage.domain.ConShareManage;
import com.slz3.business.module.drawManagement.draIntegralLottery.domain.DraIntegralLottery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

/**
 * @author lijie at 2018/11/27
 * @version 1.0
 */
@Slf4j
public class ConShareManagePGToRedisSync {
    public static void sync(RedisTemplate redisTemplate, List<DomainEntity> validAll) {

        Gson gson=new Gson();
        List<ConShareManage> domainList = validAll.stream().map(item -> (ConShareManage) item).collect(toList());

        redisTemplate.delete(RK.SERVICE_SHARE_MANAGE_SHARE_OBJECT_UUID_$UUID$.replace("${uuid}", "*"));

        if(!CollectionUtils.isEmpty(validAll)) {
            Map<String,String> manageMap=new HashMap<>();
            for(ConShareManage conShareManage:domainList){
                manageMap.put(getKey(conShareManage.getFkUUID()),gson.toJson(conShareManage));
            }
            redisTemplate.opsForValue().multiSet(manageMap);
        }



        Object res=redisTemplate.opsForValue().get(getKey(domainList.get(0).getFkUUID()));

        log.info(gson.toJson(res));
    }

    private static String getKey(UUID uuid) {
        String key= RK.SERVICE_SHARE_MANAGE_SHARE_OBJECT_UUID_$UUID$.replace("${uuid}", uuid.toString());
        log.info(key);
        return key;
    }
}
